<%@page import="message.FileWasChanged"%>
<%@ page language="java" session="true" isThreadSafe="true" contentType="text/html; charset=ISO-8859-1"%>
<%@ page import="java.io.*" %>
<%@ page import="serverDB.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.zip.CRC32"%>
<%@ page import="java.util.zip.Checksum" %>
<%@ page import="serverManger.Server" %>
<%@ page import="java.util.Arrays" %>

<%
		String boxName;
		boxName=request.getParameter("boxName");
        //to get the content type information from JSP Request Header
        String contentType = request.getContentType();
        //here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
        if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
                DataInputStream in = new DataInputStream(request.getInputStream());
                //we are taking the length of Content type data
                int formDataLength = request.getContentLength();
                byte dataBytes[] = new byte[formDataLength];
                int byteRead = 0;
                int totalBytesRead = 0;
                //this loop converting the uploaded file into byte code
                while (totalBytesRead < formDataLength) {
                        byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                        totalBytesRead += byteRead;
                        }
                                        String file = new String(dataBytes);
                                        System.out.println(file);
                                       
                                        System.out.println(request.getParameter("id"));
                //for saving the file name
                
                 String saveFile = file.substring(file.indexOf("filename=\"") + 10);
                saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
                saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
                int lastIndex = contentType.lastIndexOf("=");
                String boundary = contentType.substring(lastIndex + 1,contentType.length());
                int pos;
                
                //extracting the index of file 
                pos = file.indexOf("filename=\"");
                pos = file.indexOf("\n", pos) + 1;
                pos = file.indexOf("\n", pos) + 1;
                pos = file.indexOf("\n", pos) + 1;
                int boundaryLocation = file.indexOf(boundary, pos) - 4;
                int startPos = ((file.substring(0, pos)).getBytes()).length;
                int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
                int boxPos = file.indexOf("name=\"boxName\"");
                boxPos = file.indexOf("\n", boxPos) + 1;
                boxPos = file.indexOf("\n", boxPos) + 1;

   
                boxName=file.substring(boxPos);
         		//System.out.println("barak : \n"+boxName);
         		boxName=boxName.substring(0,boxName.indexOf("\n"));
                ConnectToDataBase connectToDataBase = ConnectToDataBase.getInst();
                List<Client> list=connectToDataBase.getClient2("from Client where user_name= '"+session.getAttribute("userName")+"'");
                Checksum checksum = new CRC32();
                checksum.update(dataBytes,startPos,(endPos - startPos));
                Server server = Server.getServerInst();
                
                //System.out.println("boxName in jsp: " +boxName);
                serverDB.File DBfile = null; 
                
                if(server.fileWasChanged(list.get(0),saveFile,checksum.getValue(),(endPos - startPos),boxName,null,new FileWasChanged(saveFile,checksum.getValue(),(endPos - startPos),boxName))){
                	DBfile = server.createFile(saveFile,Arrays.copyOfRange(dataBytes, startPos, endPos),boxName,list.get(0),checksum.getValue());
                	server.updateOtherComp2(list.get(0),null,new FileWasChanged(saveFile,checksum.getValue(),(endPos - startPos),boxName));
                }
                
                %><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>
                <% out.println(saveFile); %></td></tr></table> <%
                }
%>